/**
 * 广寒宫
 * 网址:www.guanghangong.xyz
 */
package org.moon.framework.sample.oss.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.moon.framework.autoconfigure.MoonConstants;
import org.moon.framework.autoconfigure.cache.CacheService;
import org.moon.framework.autoconfigure.oss.OssTemplate;
import org.moon.framework.autoconfigure.oss.props.OssProperties;
import org.moon.framework.autoconfigure.secure.annotation.FormRepeat;
import org.moon.framework.autoconfigure.secure.annotation.PreAuth;
import org.moon.framework.autoconfigure.springmvc.response.R;
import org.moon.framework.autoconfigure.syslog.annotation.SysLog;
import org.moon.framework.autoconfigure.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * oss相关测试
 * @author moon
 */
@RestController
@Api(tags = "oss测试")
public class MyController {

    @Autowired
    private CacheService cacheService;

    @PostMapping(value = "/local")
    @ApiOperation(value = "local测试")
    public R<Void> local(@RequestParam("file") MultipartFile file) {
        setEnv(MoonConstants.OssTypeEnum.local,"127.0.0.1:8086",null,null,"d:\\moon");
        OssTemplate.getOssTemplate().putFile(file.getOriginalFilename(),file);
        return R.success();
    }

    @PostMapping(value = "/scp")
    @ApiOperation(value = "scp测试")
    public R<Void> scp(@RequestParam("file") MultipartFile file) {
        setEnv(MoonConstants.OssTypeEnum.scp,"127.0.0.1","root","123456","d:\\moon");
        OssTemplate.getOssTemplate().putFile(file.getOriginalFilename(),file);
        return R.success();
    }

    @PostMapping(value = "/minio")
    @ApiOperation(value = "minio")
    public R<Void> minio(@RequestParam("file") MultipartFile file) {
        setEnv(MoonConstants.OssTypeEnum.minio,"192.168.3.8:9000","minioadmin","minioadmin","moon");
        OssTemplate.getOssTemplate().putFile(file.getOriginalFilename(),file);
        return R.success();
    }

    @PostMapping(value = "/aliyun")
    @ApiOperation(value = "aliyun")
    public R<Void> aliyun(@RequestParam("file") MultipartFile file) {
        setEnv(MoonConstants.OssTypeEnum.aliyun,"127.0.0.1","aliyun","aliyun","moon");
        OssTemplate.getOssTemplate().putFile(file.getOriginalFilename(),file);
        return R.success();
    }

    @PostMapping(value = "/qiniu")
    @ApiOperation(value = "qiniu")
    public R<Void> qiniu(@RequestParam("file") MultipartFile file) {
        setEnv(MoonConstants.OssTypeEnum.qiniu,"127.0.0.1","qiniu","qiniu","moon");
        OssTemplate.getOssTemplate().putFile(file.getOriginalFilename(),file);
        return R.success();
    }

    /**
     * 设置存储环境，正式使用的时候不用这么着，读取数据库配置就行
     * @param type oss类型
     * @param endpoint  存储url
     * @param accessKey 用户名
     * @param secretKey 密码
     * @param bucketName  存储目录
     */
    private void setEnv(MoonConstants.OssTypeEnum type,String endpoint,String accessKey,String secretKey,String bucketName){
        OssProperties ossProperties = new OssProperties();
        ossProperties.setName(type.name());
        ossProperties.setEndpoint(endpoint);
        ossProperties.setAccessKey(accessKey);
        ossProperties.setSecretKey(secretKey);
        ossProperties.setBucketName(bucketName);
        cacheService.set(OssTemplate.CACHE_OSS_CONFIG,ossProperties);
    }
}